import { create, update, remove, query } from '../../../framework/service/index';
import { ToastTips } from '../../../common/toastTips/tosat';

const delay = timeout => {
  return new Promise(resolve => {
    setTimeout(resolve, timeout);
  });
};

export default {

  namespace: 'register',

  state: {
    info:''
  },

  subscriptions: {
    setup({ dispatch, history }) {  // eslint-disable-line
      history.listen(location => {
        if(location.pathname === '/register') {
          dispatch({
            type: 'createAccount',
          })
        }
      })
    },
  },

  effects: {
    *createAccount({ payload }, { call, put, select }){

      const { code, data, message } = yield call(create, '/oauth/app/registerApplicant', payload.data);
      console.log('code', JSON.stringify(code));
      console.log('data', JSON.stringify(data));
      console.log('message', JSON.stringify(message));
      if (code === 200) {
        ToastTips("注册成功，正在跳转", 2000, null);
        const loginData = {
          account:payload.data.account,
          password:payload.data.password,
          isRegister:true,
        }
        yield call(delay, 1000);
        yield put({
          type:'login/postLogin',
          payload:{
            data:loginData,
          }
        })
      }else{
        ToastTips("注册失败", 2000, null);
      }
    }
  },

  reducers: {
    save(state, action) {
      return { ...state, ...action.payload };
    },
  },

};
